<template>
  <div class="common-layout">
    <el-container>
      <el-header class="header-wrapper">
        <div class="header-content">
          <h3>通用后台管理系统</h3>
          <span class="welcome">欢迎你的登录</span>
        </div>
      </el-header>
      <el-main>
        <div class="login-wrapped">
          <el-card class="box-card">
            <el-tabs v-model="activeName" class="demo-tabs" :stretch="true">
              <!--登录表单-->
              <el-tab-pane label="登录" name="first">
                <el-form class="login-form">
                  <el-form-item label="账号">
                    <el-input v-model="loginData.account" placeholder="请输入账号" />
                  </el-form-item>
                  <el-form-item label="密码">
                    <el-input v-model="loginData.password" placeholder="请输入密码" type="password" />
                  </el-form-item>
                  <!--输入框底部内容-->
                  <div class="foot-wrapped">
                    <div class="forget-password">
                      <span class="forget-password-button" @click="openForget">忘记密码？</span>
                    </div>
                  </div>
                  <div class="foot-button">
                    <el-button type="primary" @click="Login">登录</el-button>
                  </div>
                  <div class="foot-go-register">
                    还没有账号?<span class="go-register">去注册</span>
                  </div>
                </el-form>
              </el-tab-pane>
              <!--注册表单-->
              <el-tab-pane label="注册" name="second">
                <el-form class="register-form">
                  <el-form-item label="账号">
                    <el-input v-model="registerData.account" placeholder="账号由6-16位字母或数字组成" />
                  </el-form-item>
                  <el-form-item label="密码 ">
                    <el-input v-model="registerData.password" placeholder="请输入密码" />
                  </el-form-item>
                  <el-form-item label="确认密码">
                      <el-input v-model="registerData.repassword" placeholder=" 请再次输入密码"/>
                    </el-form-item>
                </el-form>
                <div class="foot-button">
                  <el-button type="primary" @click="Register">注册</el-button>
                </div>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </div>
      </el-main>
      <el-footer class="footer-wrapper">
        <div class="footer-content">
          <div class="title">
            <span>Copyright © 2025</span>|
            <span>移动云倾情赞助</span>|
            <span>布拉德澈作品</span>
          </div>

        </div>
      </el-footer>
    </el-container>
  </div>
  <forget ref="forgetP"></forget>
</template>

<script lang="ts" setup>
import {
  reactive,
  ref,
} from 'vue';

import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

import {
  login,
  register,
} from '@/api/login.js';

//导入忘记密码功能
import forget from './components/foeget_passWord.vue';

const activeName = ref('first')
const router = useRouter()
const forgetP = ref()
const openForget = () => {
  forgetP.value.open()
}


interface formData {
  account: string;
  password: string;
  repassword?: string;
}
const loginData: formData = reactive({
  account: '',
  password: '',
})
const registerData: formData = reactive({
  account: '',
  password: '',
  repassword: '',
})

//注册功能
const Register = async () => {
  if(registerData.password == registerData.repassword){
    const res = await register(registerData)
    if(res.data.message == '注册用户成功'){
      ElMessage({
        message: '恭喜！注册成功',
        type: 'success',
      })
      activeName.value = 'first'
    } else {
      ElMessage.error('注册失败，请确认账号是否已被注册')
    }
  } else {
    ElMessage.error('两次输入的密码不一致')
  }
}
//登录功能
const Login = async () => {
  const res = await login(loginData)
  const {token} = res.data
  if(res.data.message == '登录成功'){
    ElMessage({
      message: '登录成功',
      type: 'success',
    })
    localStorage.setItem('token', token)
    router.push('/home')
  } else {
    ElMessage.error('登录失败，请确认账号和密码是否正确')
  }
}
  
 
</script>
<style lang="scss" scoped>
.header-wrapper {
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 1200px;
    margin: 0 auto;

    .welcome {
      font-size: 14px;
    }
  }
}

//页面主题样式
.el-main {
  background-image: url('@/assets/code.png');
  height: 600px;
  --el-main-padding: 0;

  .login-wrapped {
    width: 1200px;
    height: 600px;
    margin: 0 auto;

    .box-card {
      width: 350px;
      height: 375px;
      float: right;
      position: relative;
      top: 14%;

      .login-form {
        .foot-wrapped {
          display: flex;
          flex-direction: column;
          margin-top: 10px 0;

          .forget-password {
            display: flex;
            justify-content: flex-end;
            margin: 10px 0;

            .forget-password-button {
              font-size: 12px;
              color: #409eff;
              cursor: pointer;
            }
          }
        }

        .foot-go-register {
          font-size: 12px;
          margin: 12px 0;
          display: flex;
          justify-content: center;

          .go-register {
            font-size: 12px;
            color: #409eff;
            cursor: pointer;
          }
        }

        .foot-button {
          width: 100%;
          display: flex;
          justify-content: center;
        }
      }
    }

    .el-form {
      margin-top: 30px;
    }
  }
}

.footer-wrapper {
  margin-top: 18px;

  .footer-content {
    margin: 0 auto;
    width: 1200px;
    display: flex;
    justify-content: center;
    align-items: center;

    .title {
      color: #666;

    }

    span {
      font-size: 12px;
      color: #666;
      margin-right: 6px;
    }
  }
}

//样式穿透表单变局，输入框大小和标签高度
:deep(.el-tabs__item) {
  color: #333;
  font-size: 16px;
}

:deep(.el-input__inner) {
  height: 40px;
}

:deep(.el-form-item__label) {
  height: 40px;
  line-height: 40px;
}

:deep(.el-button) {
  height: 40px;
  width: 300px;
  font-size: 16px;
}
</style>